package com.wejoy.video.middle.station.data.service.workorder.impl;

import com.wejoy.video.middle.station.data.domain.workorder.OrderAlarmInfo;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.workorder.IOrderAlarmInfoMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.workorder.IOrderAlarmInfoService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工单告警类型
 *
 * @author miracle_j
 * @date 2022年11月27日 21:57:01
 */
@Service
public class OrderAlarmInfoServiceImpl implements IOrderAlarmInfoService {
    @Autowired
    private IOrderAlarmInfoMapper orderAlarmInfoMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(OrderAlarmInfo req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(OrderAlarmInfo req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return orderAlarmInfoMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(OrderAlarmInfo orderAlarmInfo) {
        WJAssert.notNull(orderAlarmInfo, "入参对象不能为空");
        WJAssert.notNull(orderAlarmInfo.getId(), "id参数错误");
        OrderAlarmInfo where = new OrderAlarmInfo();
        where.setId(orderAlarmInfo.getId());
        return orderAlarmInfoMapper.updateSelective(orderAlarmInfo, where);
    }

    @Override
    public int getCount(OrderAlarmInfo orderAlarmInfo) {
        checkStringLength(orderAlarmInfo, true);
        return orderAlarmInfoMapper.getCount(orderAlarmInfo);
    }

    @Override
    public OrderAlarmInfo getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        OrderAlarmInfo where = new OrderAlarmInfo();
        where.setId(id);
        return orderAlarmInfoMapper.getOne(where);
    }

    @Override
    public List<OrderAlarmInfo> getList(OrderAlarmInfo orderAlarmInfo) {
        checkStringLength(orderAlarmInfo, true);
        return orderAlarmInfoMapper.getList(orderAlarmInfo);
    }

    @Override
    public List<OrderAlarmInfo> getPageList(OrderAlarmInfo orderAlarmInfo) {
        checkStringLength(orderAlarmInfo, true);
        return orderAlarmInfoMapper.getPageList(orderAlarmInfo);
    }

    @Override
    public void audit(Long id, String operator) {
        OrderAlarmInfo req = new OrderAlarmInfo();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        OrderAlarmInfo req = new OrderAlarmInfo();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        OrderAlarmInfo req = new OrderAlarmInfo();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(OrderAlarmInfo orderAlarmInfo, boolean nullAble) {
        WJAssert.notNull(orderAlarmInfo, "入参对象不能为空");
        WJAssert.limitMaxLength(orderAlarmInfo.getName(), true, 50, "告警类型不合法!");
        WJAssert.limitMaxLength(orderAlarmInfo.getCallBackName(), true, 50, "EVScall_back_name不合法!");
        WJAssert.limitMaxLength(orderAlarmInfo.getMainTypeName(), true, 50, "大类名称不合法!");
        WJAssert.limitMaxLength(orderAlarmInfo.getMinorTypeName(), true, 50, "小类名称不合法!");
        WJAssert.limitMaxLength(orderAlarmInfo.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(orderAlarmInfo.getRemark(), true, 255, "备注不合法!");
    }
}
